<template>
  <!-- 退货原因设置弹出层 -->
  <div class=''>
    <el-dialog :model-value="props.orderVisiable" :show-close="true" :title="props.itemId === 0 ? '添加退货原因'
      : '编辑退货原因'" width=" 40%" :before-close="handleClose">
      <el-form :model="reasonForm" label-width="120px">
        <el-form-item label="原因类型">
          <el-input v-model="reasonForm.name" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"
            placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="reasonForm.sort" type="text" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="reasonForm.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
const reasonForm = ref({
  name: "",
  sort: 0,
  status: 1,
  createTime: ""
})
const props = defineProps(["orderVisiable", "itemId"])
import * as returnReason from "@/request/oms_returnReason"
import { ElMessage } from 'element-plus';
const emits = defineEmits()
const handleClose = () => {
  emits("closeReasonDlg")
}
watch(() => props.itemId, () => {
  if (props.itemId !== 0) {
    returnReason.getOnlyReturnReasonByI(props.itemId).then(res => {
      if (res.code === 200) {
        reasonForm.value.name = res.data.name
        reasonForm.value.status = res.data.status
        reasonForm.value.createTime = res.data.createTime
      }
    })
  }
})
const submit = () => {
  if (props.itemId === 0) {
    returnReason.addReason(reasonForm.value).then(res => {
      if (res.code === 200) {
        ElMessage.success("新增成功")
      } else {
        ElMessage.error(res.message)
      }
    }).finally(() => {
      emits("closeReasonDlg")
      emits("getAdminList")
      resetForm()
    })
  } else {
    returnReason.updateReasonById({ id: props.itemId, returnReason: reasonForm.value }).then(res => {
      if (res.code === 200) {
        ElMessage.success("修改成功")
      } else {
        ElMessage.error(res.message)
      }
    }).finally(() => {
      emits("closeReasonDlg")
      emits("getAdminList")
      resetForm()
    })
  }

}
// 清空数据
const resetForm = () => {
  reasonForm.value.name = ""
  reasonForm.value.status = 1;
  reasonForm.value.createTime = "";
  reasonForm.value.sort = 0;
}
</script>

<style lang="less" scoped></style>